<template>
  <div>
    <Alert type="success">
      资源授权管理
      <template slot="desc">系统资源以角色为基本单位授权，角色授权支持权限继承，新增角色继承自默认角色“default”。 </template>
    </Alert>
    <Form ref="formInline" :model="map" inline>
      <div class="search">
        <Button type="primary" class="fl" icon="plus" @click="add">增加权限</Button>
        <FormItem>
          <Select v-model="map.role_id" style="width:200px;">
            <Option
              v-for="item in roleList"
              :value="item.value"
              :key="item.value"
            >{{ item.label }}
            </Option>
          </Select>
        </FormItem>
        <FormItem>
          <Input type="text" v-model="map.key" placeholder="关键字"></Input>
        </FormItem>
        <FormItem>
          <Button type="primary" @click="getList">查询</Button>
        </FormItem>
      </div>
    </Form>
    <Table border stripe :loading="loading" :columns="columns" :data="data && data.data"></Table>
    <Page class="page" :total="data && data.count" :page-size="data && data.pagesize" show-total @on-change="changePage"></Page>

  </div>
</template>
<script>
  import {role, roleAuth} from "@/api";
  import {Button, Form, FormItem, Input, Page, Table,Select,Option,Alert} from 'iview';

  export default {
    components: {
      Button,
      Table,
      Page,
      Form,
      FormItem,
      Input,
      Select,
      Option,
      Alert
    },
    data() {
      return {
        loading: true,
        columns: [
          {
            title: "角色",
            key: "role_id",
            width: 150,
            align: "center",
            render: (h, params) => {
              return h('span', params.row.roleInfo['title']);
            }
          },
          {
            title: "资源",
            key: "resource_id",
            width: 150,
            align: "center",
            render: (h, params) => {
              return h('span', params.row.resourceInfo['title']);
            }
          },
          {
            title: "路由",
            key: "resource_id",
            width: 150,
            align: "center",
            render: (h, params) => {
              return h('span', params.row.resourceInfo['path']);
            }
          },
          {
            title: "GET",
            key: "get",
            //width: 400,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "i-switch",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      value: params.row.get === 1
                    },
                    style: {
                      //marginRight: "5px"
                    },
                    on: {
                      "on-change": (status) => {
                        const id = params.row.id;
                        let data = {id: id, get: status ? 1 : 0};
                        roleAuth.update(id, data)
                      }
                    }
                  },
                ),
              ]);
            }
          },

          {
            title: "POST",
            key: "post",
            //width: 400,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "i-switch",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      value: params.row.post === 1
                    },
                    style: {
                      //marginRight: "5px"
                    },
                    on: {
                      "on-change": (status) => {
                        const id = params.row.id;
                        let data = {id: id, post: status ? 1 : 0};
                        roleAuth.update(id, data)
                      }
                    }
                  },
                ),
              ]);
            }
          },
          {
            title: "PUT",
            key: "put",
            //width: 400,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "i-switch",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      value: params.row.put === 1
                    },
                    style: {
                      //marginRight: "5px"
                    },
                    on: {
                      "on-change": (status) => {
                        const id = params.row.id;
                        let data = {id: id, put: status ? 1 : 0};
                        roleAuth.update(id, data)
                      }
                    }
                  },
                ),
              ]);
            }
          },
          {
            title: "DELETE",
            key: "delete",
            //width: 400,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "i-switch",
                  {
                    props: {
                      type: "primary",
                      size: "small",
                      value: params.row.delete === 1
                    },
                    style: {
                      //marginRight: "5px"
                    },
                    on: {
                      "on-change": (status) => {
                        const id = params.row.id;
                        let data = {id: id, delete: status ? 1 : 0};
                        roleAuth.update(id, data)
                      }
                    }
                  },
                ),
              ]);
            }
          },
          {
            title: "操作",
            key: "action",
            width: 100,
            align: "center",
            render: (h, params) => {
              return h("div", [
                h(
                  "Button",
                  {
                    props: {
                      type: "error",
                      size: "small",
                      // icon:'trash-a'
                    },
                    on: {
                      click: () => {
                        if(confirm("确定要删除吗？")) {
                          this.delete(params.row.id, params.index);
                        }
                      }
                    },

                  },
                  '删除'
                )
              ]);
            }
          }
        ],
        roleList:[],
        data: {},
        map: {
          role_id: 0,
          page: 1,
          key: "",
          pageSize: 10,
        }
      };
    },
    methods: {
      getList() {
        this.loading = true;
        roleAuth.getList(this.map).then(res => {
          this.data = res.data;
          this.loading = false;
        });
      },
      delete(id, index) {
        roleAuth.delete(id).then(res => {
          // this.data.splice(index, 1);
          this.getList();
        });
      },
      changePage(index) {
        this.map.page = index;
        this.getList();
      },
      add() {
        this.$router.push({
          path: "/ucenter/auth/save",
          query: {
            role_id: this.map.role_id
          }
        });
      },
      async getRoleList() {
        this.loading = true;
        const map = {page: 0};
        let data = await role.getList(map);
        this.roleList = data.data.map(item => {
          return {
            value: item.id,
            label: `[${item.name}]${item.title}`
          };
        });
        this.roleList.unshift({value: "", label: "请选择角色"});
        this.loading = false;
      },
    },
    mounted() {
      if(this.$route.query.role_id) {
        this.map.role_id = this.$route.query.role_id;
      }

      this.getList();
      this.getRoleList();
    }
  };
</script>
